// expensecard.js 消费卡购买
var shihan = require('../../../common/fetch')
var config = require('../../../common/config')
var app = getApp();
var range
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    channelid: '',
    amount: '',
    payflag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    range = 840 / winScale;
    var animation = wx.createAnimation({
      duration: 350,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;

    shihan.sendRequest(config.channel_get, 'POST', {
        channel_id: options.channelid
      })
      .then(function(response) {
        console.log(response);
        that.setData({
          soreinfo: response.data.data,
          channelid: options.channelid
        })
      }, function(error) {
        console.log(error);
      });
  },
  inputmoner: function(e) {
    this.setData({
      amount: e.detail.value
    })
  },
  formSubmit: function(e) {
    // console.log(e)
    var that = this
    shihan.sendRequest(config.buy_consumer_card, 'POST', {
        channel_id: this.data.channelid,
        amount: this.data.amount
      })
      .then(function(response) {
        if (response.data.code == 0) {
          var cardno = response.data.data.sh_trade_no;
          wx.login({
            success: function(res) {
              shihan.sendRequest(config.card_wx_perpay, 'POST', {
                  trade_no: response.data.data.sh_trade_no,
                  code: res.code
                })
                .then(function(response) {
                  if (response.data.code == 0) {
                    var prepayid = response.data.data.sh_package;
                    wx.requestPayment({
                      "appId": response.data.data.sh_appid,
                      'timeStamp': new String(response.data.data.sh_timestamp),
                      'nonceStr': response.data.data.sh_noncestr,
                      'package': response.data.data.sh_package,
                      'signType': 'MD5',
                      'paySign': response.data.data.sh_sign,
                      'success': function(res) {
                        app.submissionForm(prepayid.split('=')[1], '3', '4')
                        that.animation.translateY(-range).step();
                        that.setData({
                          payflag: true,
                          cardno: cardno,
                          animationData: that.animation.export()
                        })
                      },
                      'fail': function(res) {
                        app.submissionForm(e.detail.formId, '1', '3')
                        console.log(res)
                      }
                    })
                  } else {
                    shihan.showToast.call(that, response.data.msg);
                  }
                }, function(error) {
                  console.log(error);
                });
            }
          })
        } else {
          shihan.showToast.call(that, response.data.msg);
        }
      }, function(error) {
        shihan.showToast.call(that, '网络错误，请稍后重试');
      });
  },
  lookdetail: function(e) {
    wx.redirectTo({
      url: '/pages/other/expensecarddetail/expensecarddetail?tradeno=' + e.currentTarget.dataset.no,
    })
    this.animation.translateY(range).step();
    this.setData({
      payflag: false,
      animationData: this.animation.export()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})